package com.example.appkcsj.slice;

import com.example.appkcsj.ResourceTable;
import com.example.appkcsj.dbutils.DBUtils;
import com.example.appkcsj.entity.Student;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;


public class secondAbilitySlice extends AbilitySlice implements Component.ClickedListener{
    private final static HiLogLabel HI_LOG_LABEL=
            new HiLogLabel(HiLog.LOG_APP,0x00201,"secondAbilitySlice");
    private TextField username1;
    private TextField password1;
    private TextField password2;
    private Button reg1;
    private Button reg2;
    private DBUtils dbUtils;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_second);

        dbUtils=new DBUtils(secondAbilitySlice.this);
        username1= (TextField) findComponentById(ResourceTable.Id_field3);
        password1= (TextField) findComponentById(ResourceTable.Id_field4);
        password2= (TextField) findComponentById(ResourceTable.Id_field5);
        reg1 =(Button)findComponentById(ResourceTable.Id_but3);
        reg1.setClickedListener(this);
        reg1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(component == reg1){
                    if("".equals(username1.getText())||"".equals(password1.getText()))
                    {
                        ToastDialog td = new ToastDialog(secondAbilitySlice.this);
                        td.setText("请输入用户名及密码！");
                        td.show();
                    }
                    else {
                        if (password2.getText().equals(password1.getText())) {
                            Student student = new Student(username1.getText(), password1.getText(), password2.getText());
                            boolean flag = dbUtils.insertStudent(student);
                            if (flag) {
                                HiLog.info(HI_LOG_LABEL, "--插入数据->>" + flag);
                                ToastDialog td = new ToastDialog(secondAbilitySlice.this);
                                td.setText("注册成功！");
                                td.show();
                                Intent m = new Intent();
                                m.setParam("username1",username1.getText());
                                m.setParam("password1",password1.getText());
                                m.setParam("password2",password2.getText());
                                present(new MainAbilitySlice(),m);
                                Operation operation = new Intent.OperationBuilder()
                                        .withDeviceId("")
                                        .withBundleName("com.example.appkcsj")//跳转到哪个应用
                                        .withAbilityName("com.example.appkcsj.MainAbility")//要跳转的包名
                                        .build();//将上面的信息打包
                                m.setOperation(operation);
                                startAbility(m);
                            } else {
                                HiLog.info(HI_LOG_LABEL, "--插入数据->>" + flag);
                                ToastDialog td = new ToastDialog(secondAbilitySlice.this);
                                td.setText("注册失败！");
                                td.show();
                            }
                        }
                        else{
                            if("".equals(password2.getText())){
                                ToastDialog td = new ToastDialog(secondAbilitySlice.this);
                                td.setText("请确认密码！");
                                td.show();
                            }
                            else{
                                ToastDialog td = new ToastDialog(secondAbilitySlice.this);
                                td.setText("两次密码不一致！");
                                td.show();}
                        }
                    }
                } }
        });

    }
    @Override
    public void onActive() {
        super.onActive();
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    @Override
    public void onClick(Component component) {}
}